/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ShopBankCard;
import com.yami.shop.bean.model.ShopWallet;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.ShopBankCardMapper;
import com.yami.shop.dao.ShopWalletMapper;
import com.yami.shop.service.ShopBankCardService;
import com.yami.shop.service.ShopWalletService;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商家钱包信息
 *
 * @author YXF
 *  * @date 2020-04-07 14:22:08
 */
@Service
@AllArgsConstructor
public class ShopBankCardServiceImpl extends ServiceImpl<ShopBankCardMapper, ShopBankCard> implements ShopBankCardService {
    private final ShopBankCardMapper shopBankCardMappping;
    private final Integer NOT_DEFAULT = 0;
    private final Integer DEFAULT = 1;

    @Override
    public void insert(ShopBankCard shopBankCard) {
        //银行卡卡号重复判断
        int cardCount = shopBankCardMappping.selectCount(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getCardNo,shopBankCard.getCardNo())
                                                                                                .eq(ShopBankCard::getShopId,shopBankCard.getShopId()));
        if (cardCount > 0) {
            throw new YamiShopBindException("yami.shop.card.is.exist");
        }
        int count = shopBankCardMappping.selectCount(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopBankCard.getShopId()));
        if (count == 0){
            shopBankCard.setIsDefault(DEFAULT);
        }else {
            shopBankCard.setIsDefault(NOT_DEFAULT);
        }
        shopBankCardMappping.insert(shopBankCard);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefault(Long shopBankCardId, Long shopId) {
        ShopBankCard shopBankCard = new ShopBankCard();
        shopBankCard.setIsDefault(NOT_DEFAULT);
        shopBankCardMappping.update(shopBankCard,new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId,shopId));
        ShopBankCard shopBankCardDb = shopBankCardMappping.selectById(shopBankCardId);
        shopBankCardDb.setIsDefault(DEFAULT);
        shopBankCardMappping.updateById(shopBankCardDb);
    }

}
